package com.bawei.lib_common.widget

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView

/**
 * 解决滑动冲突的RecyclerView
 */
class ConflictFreeRecyclerView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : RecyclerView(context, attrs, defStyleAttr) {
    
    private var startX = 0f
    private var startY = 0f
    private var isHorizontalScroll = false
    private val touchSlop = 10f
    
    init {
        setupScrollConflict()
    }
    
    /**
     * 设置滑动冲突处理
     */
    private fun setupScrollConflict() {
        addOnItemTouchListener(object : OnItemTouchListener {
            override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
                when (e.action) {
                    MotionEvent.ACTION_DOWN -> {
                        startX = e.x
                        startY = e.y
                        isHorizontalScroll = false
                    }
                    MotionEvent.ACTION_MOVE -> {
                        if (!isHorizontalScroll) {
                            val deltaX = Math.abs(e.x - startX)
                            val deltaY = Math.abs(e.y - startY)
                            
                            // 判断滑动方向
                            val layoutManager = layoutManager
                            val isHorizontal = layoutManager?.canScrollHorizontally() == true
                            
                            if (isHorizontal) {
                                // 水平滚动：如果水平滑动距离大于垂直滑动距离且超过阈值，拦截事件
                                if (deltaX > deltaY && deltaX > touchSlop) {
                                    isHorizontalScroll = true
                                    return true
                                } else if (deltaY > deltaX && deltaY > touchSlop) {
                                    // 垂直滑动，不拦截
                                    return false
                                }
                            } else {
                                // 垂直滚动：如果垂直滑动距离大于水平滑动距离且超过阈值，拦截事件
                                if (deltaY > deltaX && deltaY > touchSlop) {
                                    isHorizontalScroll = true
                                    return true
                                } else if (deltaX > deltaY && deltaX > touchSlop) {
                                    // 水平滑动，不拦截
                                    return false
                                }
                            }
                        }
                    }
                    MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                        isHorizontalScroll = false
                    }
                }
                return false
            }
            
            override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {
                // 不需要处理
            }
            
            override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
                // 不需要处理
            }
        })
    }
    
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                startX = event.x
                startY = event.y
            }
            MotionEvent.ACTION_MOVE -> {
                val deltaX = Math.abs(event.x - startX)
                val deltaY = Math.abs(event.y - startY)
                
                val layoutManager = layoutManager
                val isHorizontal = layoutManager?.canScrollHorizontally() == true
                
                if (isHorizontal) {
                    // 如果是水平滑动且滑动距离足够大，阻止父容器拦截
                    if (deltaX > deltaY && deltaX > touchSlop) {
                        parent?.requestDisallowInterceptTouchEvent(true)
                    }
                } else {
                    // 如果是垂直滑动且滑动距离足够大，阻止父容器拦截
                    if (deltaY > deltaX && deltaY > touchSlop) {
                        parent?.requestDisallowInterceptTouchEvent(true)
                    }
                }
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                // 恢复父容器的触摸拦截
                parent?.requestDisallowInterceptTouchEvent(false)
            }
        }
        return super.onTouchEvent(event)
    }
} 